define(["jquery","comm/form/form.attention","comm/form/form.validate"],function($,$attention,$validate){
	var quest = {};
	
	/**
	 * 数据处理(若不需要数据处理，可以不写)
	 */
    quest.formatData = function(obj){
    	if( !obj.maxWeek ) obj.maxWeek = 45;
    	if( !obj.minWeek ) obj.minWeek = 20;
    	var week = [];
    	for (var i = obj.minWeek; i <= obj.maxWeek; i++) {
    		week.push(i);
    	}
    	obj.list = [{
			"subunit":"周",
			"sublist":week
		},{
			"subunit":"天",
			"sublist":[0,1,2,3,4,5,6]
		}];
    	var len = obj.list.length;
    	obj.width = (100 / len) + "%";
    	for (var i = 0; i < len; i++) {
    		obj.list[i].textWidth = Number(obj.list[i].subunit.length) + Number(1) + 'em';
    	}
		return obj;
	}

	/**
	 * tpl模板
	 */
	quest.tpl = function(){
		return 	'<script id="tpl_question_gestation" type="text/html">'+
				'	<div {{if relationId}}relationId="{{relationId}}"{{/if}} class="qaCard-gestation qaCard form-group form-group-sm col-sm-{{length}} col-md-{{length}} grid-label-{{labelLength}}">'+
				'		<label class="control-label {{align}}">{{label}}</label>'+
				'   	<div class="control-input clearfix">'+
				'			{{if status == "default"}}'+
				'				{{each list as item i}}'+
				'					<div class="multiple" style="width:{{width}}">'+
				'						<select class="form-control validatebox" style="width:calc(100% - {{item.textWidth}} - 4px)" name="{{name}}" data-type="{{type}}" data-quesId="{{id}}" data-innDeCode="{{innDeCode}}" data-deCode="{{deCode}}" data-dataUnit="{{dataUnit}}" data-validate="{{validate}}" data-attention="{{attention}}">'+
				'							{{each item.sublist as value i}}'+
				'								<option value="{{value}}">{{value}}</option>'+
				'							{{/each}}'+
				'						</select>'+
				'						<span style="width:{{item.textWidth}}">{{item.subunit}}</span>'+
				'					</div>'+
				'				{{/each}}'+
				'			{{else if status == "readonly"}}'+
				'				{{each list as item i}}'+
				'					<div class="multiple" style="width:{{width}}">'+
				'						<select class="form-control validatebox" style="width:calc(100% - {{item.textWidth}} - 4px)" name="{{name}}" data-type="{{type}}" data-quesId="{{id}}" data-innDeCode="{{innDeCode}}" data-deCode="{{deCode}}" data-attention="{{attention}}" data-validate="{{validate}}" data-attention="{{attention}}" disabled>'+
				'							{{each item.sublist as value i}}'+
				'								<option value="{{value}}">{{value}}</option>'+
				'							{{/each}}'+
				'						</select>'+
				'						<span style="width:{{item.textWidth}}">{{item.subunit}}</span>'+
				'					</div>'+
				'				{{/each}}'+
				'			{{else if status == "disabled"}}'+
				'				{{each list as item i}}'+
				'					<div class="multiple" style="width:{{width}}">'+
				'						<select class="form-control validatebox" style="width:calc(100% - {{item.textWidth}} - 4px)" name="{{name}}" data-type="{{type}}" data-quesId="{{id}}" data-innDeCode="{{innDeCode}}" data-deCode="{{deCode}}" data-attention="{{attention}}" data-validate="{{validate}}" data-attention="{{attention}}" disabled>'+
				'							{{each item.sublist as value i}}'+
				'								<option value="{{value}}">{{value}}</option>'+
				'							{{/each}}'+
				'						</select>'+
				'						<span style="width:{{item.textWidth}}">{{item.subunit}}</span>'+
				'					</div>'+
				'				{{/each}}'+
				'			{{/if}}'+
				'		</div>'+
				'	</div>'+
				'</script>';
	}

	/**
	 * 赋值
	 */
	quest.setValue = function($dom,value){
		if( value ){
			var val = Number(value.quesValue);
			var week = parseInt(val / 7);
			var day = val % 7;
			$dom.eq(0).val(week);
			$dom.eq(1).val(day);
			$dom.eq(0).blur();
			$dom.eq(1).blur();
		}
	}

	/**
	 * 获取value（保存时，获取每个dom对应的答案）
	 */
	quest.getValue = function($dom,name){
		var dom = $dom.parents(".qaCard-gestation").find("select");
		var week = Number(dom.eq(0).val());
		var day = Number(dom.eq(1).val());
		var val = 7*week + day;
		var value = {};
		value.quesValue = val.toString();
		value.quesId = $dom.attr("data-quesId");
		value.deCode = $dom.attr("data-deCode");
		value.innDeCode = $dom.attr("data-innDeCode");
		var dataUnit = $dom.attr("data-dataUnit");
		if( dataUnit ) value.dataUnit = dataUnit;
		return value;
	}

	/**
	 * 提醒
	 */
	quest.attention = function(id){
        $("#" + id + " .qaCard-gestation .validatebox").bind("blur input propertychange",onAttention);
        // 初始化
        $("#" + id + " .qaCard-gestation .validatebox").blur();
	}

	/**
	 * 校验
	 * flag:
	 * 	 true:  (点击保存按钮时使用)
	 * 	 false: 
	 */
	quest.validate = function(id,flag){
        $("#" + id + " .qaCard-gestation .validatebox").bind("blur",onValidate);
        if( flag ){
        	var result = true;
        	$("#" + id + " .qaCard-gestation .validatebox").each(function(i){
        		var params = {
		            "value":getValue($(this)),
		            "dom":$(this),
		            "flag":flag,
		        };
				var success = $validate.init(params,handleByRuleValidate,handleBytypeValidate);
	            result = result && success;
	            if( !result ) return result;
	    	});
        	return result;
        }
	}

	/**
	 * 获取输入的值(validate、attention中需要用到)
	 */
	var getValue = function($dom){
		var dom = $dom.parents(".qaCard-gestation").find("select");
		var week = Number(dom.eq(0).val());
		var day = Number(dom.eq(1).val());
		var value = 7*week + day;
		return value;
	}

	/********************  attention begin  ********************/

	/**
	 * xx情况下触发的事件 (有可能是失去焦点等)
	 */
	var onAttention = function(){
		var params = {
            "value":getValue($(this)),
            "dom":$(this),
        };
		$attention.init(params,handleByRule,handleBytype);
    }

    /**
     * 根据单个规则rule获取单个规则的结果result
     * 该题型已有规则在case里拦截，公用规则从default里返回结果
     * @return Boolean [布尔值]
     */
    var handleByRule = function(obj){
    	var result;
    	switch(obj.type) {
            default:
            	result = $attention.handleByRule(obj);
        }
        return result;
    }

    /**
     * 根据提醒类型(type)执行对应的方法
     * 该题型已有提醒类型在case里拦截，公用提醒类型在default里处理
     */
    var handleBytype = function(obj){
    	if( obj.type ){
    		switch(obj.type) {
	            default:
            		$attention.handleBytype(obj);
	        }
    	}
    }

    /********************  attention end  ********************/

    /********************  validate begin  ********************/

    /**
	 * xx情况下触发的事件 (有可能是失去焦点等)
	 */
	var onValidate = function(){
		var params = {
            "value":getValue($(this)),
            "dom":$(this),
        };
		$validate.init(params,handleByRuleValidate,handleBytypeValidate);
	}

	/**
     * 根据单个规则rule获取单个规则的结果result
     * 该题型已有规则在case里拦截，公用规则从default里返回结果
     * @return Boolean [布尔值]
     */
    var handleByRuleValidate = function(obj){
    	var result = true;
    	switch(obj.type) {
            default:
            	result = $validate.handleByRule(obj);
        }
        return result;
    }

    /**
     * 根据提醒类型(type)执行对应的方法
     * 该题型已有提醒类型在case里拦截，公用提醒类型在default里处理
     */
    var handleBytypeValidate = function(obj){
    	if( obj.type ){
    		switch(obj.type) {
	            default:
            		$validate.handleBytype(obj);
	        }
    	}
    }

	/********************  validate begin  ********************/

	return quest;
});